<?php

class users_controller_users extends Base
{
    private $uQuery;

    public function  __construct()
    {
        parent:: __construct();
        $this -> uQuery = new usersQuery();
        $this -> Authenticate();
    }

    public function init()
    {
        $where['id'] = $_GET['userId'];
        $userData = $this -> common -> select('users', '*', $where);

        if(isset($userData[0]['id']) && $userData[0]['id'] != "" && $userData[0]['id'] == $_GET['userId'])
        {
            $this -> view -> assign('userData', $userData);
        }
        else
        {
            $this -> FriendlyLink(SITE_ADDRESS);
        }

        
    }

    public function myProfile()
    {
        $this -> view -> assign('navLv1', '<a href="'.SITE_ADDRESS.'profil">Profil</a>');
        if(isset($_SESSION[PREFIX]['user']['id']))
        {
            $where = array();
            $where['id'] = $_SESSION[PREFIX]['user']['id'];
            $userData = $this -> common -> select('users', '*', $where);
//            pre($_SESSION);
            $lastWatch = $this -> uQuery -> selectLastWatch($_SESSION[PREFIX]['user']['id']);

            foreach($lastWatch as $key => $value)
            {
                $lastWatch[$key]['freeName'] = $this->replace_pl_chars(str_replace(" ", "-", strtolower($value['polish_name'])));
            }

//            pre($lastWatch);

            $favouritesSerials = $this -> uQuery -> getFavouritesSerials($_SESSION[PREFIX]['user']['id']);
            foreach($favouritesSerials as $key => $value)
            {
                $favouritesSerials[$key]['linkName'] = $this->replace_pl_chars(str_replace(" ", "-", strtolower($value['polish_name'])));
                $addToFavouriteTheSameSerial = $this -> uQuery -> getTheSameFavouritesSerials($value['id'], $_SESSION[PREFIX]['user']['id']);
                $favouritesSerials[$key]['theSamePeople'] = $addToFavouriteTheSameSerial;
                $newsForSerial = $this -> uQuery -> getSerialNews($value['id']);
//                pre($addToFavouriteTheSameSerial);
            }
//            pre($favouritesSerials);
//            pre($_SESSION);
//            $favouritesSerials = $this -> uQuery -> getFavouritesUserSerials($_SESSION[PREFIX]['user']['id']);
//            pre($favouritesSerials);
            $this -> view -> assign('profile', 'true');
            $this -> view -> assign('favouritesSerials',$favouritesSerials);
            $this -> view -> assign('lastWatch',$lastWatch);
            $this -> view -> assign('userData', $userData);
        }
        else
        {
            $this -> FriendlyLink(SITE_ADDRESS);
        }
    }

    public function removeSerialFromFav()
    {
//        pre($_GET);
        if(isset($_GET['serialId']) && $_GET['serialId'])
        {
            $this -> uQuery -> deleteSerialFromFav($_GET['serialId'], $_SESSION[PREFIX]['user']['id']);
            $message['content'] = "Usunięty serial nie jest już dalej Twoim ulubionym serialem.";
            $message['type'] = "success";
            $this -> setMessage($message);
            $this -> FriendlyLink(SITE_ADDRESS."profil");
        }
        else
        {
            $this -> FriendlyLink(SITE_ADDRESS);
        }
    }

    public function editMyProfile()
    {
        $userData = array();
        $userData[0]['id'] = $_SESSION[PREFIX]['user']['id'];
        if(isset($_POST['oldPass']) && $_POST['oldPass'] != "")
        {
            $where = array();
            $where['login'] = $_SESSION[PREFIX]['user']['login'];
            $where['password'] = md5($_POST['oldPass']);
            $userData = $this -> common -> select('users', '*', $where);
            if(!isset($userData[0]['id']))
            {
                $message['form']['oldPass'] = "Podane hasło nie pasuje do tego, które znajduje się w bazie.";
            }
            elseif($_POST['newPass'] == "" || $_POST['newPassConfirm'] == "" || $_POST['newPass']!=$_POST['newPassConfirm'])
            {
                $message['form']['newPass'] = "Podane hasła różnią się od siebie, lub pola są puste.";
                $message['form']['newPassConfirm'] = "Podane hasła różnią się od siebie, lub pola są puste.";
            }
        }
        if(isset($_FILES['avatar']) && $_FILES['avatar']['name'] !="")
        {
            
            $temp = explode('/', $_FILES['avatar']['type']);
            if($temp[0] !="image")
            {
                $message['form']['avatar'] = "Podany plik nie jest zdjęciem. Dozwolone formaty to .jpg, .jpeg, .png, .gif";
            }
            elseif($_FILES['avatar']['size'] > 120000)
            {
                $message['form']['avatar'] = "Podany plik jest za duży. Maksymalna wielkośc zdjęcia to 100 kB.";
            }
        }

        if($message == null)
        {
            require_once('lib/phpthumb-latest/ThumbLib.inc.php');
            if(isset($_FILES['avatar']) && $_FILES['avatar']['name'] !="")
            {
                $name = $this -> UploadFile('avatar','public/avatars/');
                $thumb = PhpThumbFactory::create('public/avatars/'.$name);
                $thumb->adaptiveResize(80, 80);
                $thumb->save('public/avatars/'.$name);
                $el['avatar'] = $name;
            }
            if(isset($_POST['oldPass']) && $_POST['oldPass'] != "")
            {
                $el['password'] = md5($_POST['newPass']);
            }
            $this -> common -> update($el, 'users', 'id='.$userData[0]['id']);
            $message['content'] = "Twoje dane zostały zapisane.";
            $message['type'] = 'success';
            $this -> setMessage($message);
            $this -> FriendlyLink(SITE_ADDRESS.'profil');
        }
        else
        {
            $message['content'] = "Wystąpił błąd w formularzu. Aby zmienić dane profilu popraw zaznaczone błędy.";
            $message['type'] = "error";
            $this -> setMessage($message);
            $this -> putFiltr($_POST);
            $this -> FriendlyLink(SITE_ADDRESS.'profil');
        }
        pre($message);
    }
}

?>
